<?php
//header('Content-Type: text/html; charset=utf-8');
require_once("../sqlConn.php");

$cateArr = array(
    0=>"未定义",
    1=>"生活",
    2=>"软件",
    3=>"电脑",
    4=>"足球",
	5=>"二手买卖",
);
$statusArr = array(
    0=>"刚发布 召集帮忙人ing 还没有指定给任何人完成",
    1=>"已经选择承接人 但是他还没接受 想竞争的朋友抓紧了",
    2=>"选定人拒绝完成任务 发布人将可以重新选定",
    3=>"任务已经被成功领取了",
);


$action=$_GET['action'];
switch($action)
{
	case 'reqHelp':
	   reqHelp($_POST['title'],$_POST['money'],$_POST['cate'],$_POST['description'],$_POST['detail'],$_POST['username'],$_POST['userID']);
	   break;

	case 'getHelp':
	   getHelp($_POST['id']);
	   break;	

	case 'iReqList':
		iReqList($_POST['username']);
		break;

	case 'allReqList':
		allReqList();
		break;

	case 'iApplyList':
		iApplyList($_POST['userID']);
		break;

	case 'iChooseList':
		iChooseList($_POST['userID']);
		break;

	case 'iFocusList':
		iFocusList($_POST['userID']);
		break;

	case 'friendReqList':
		friendReqList($_POST['userID']);
		break;

	case 'cateReqList':
		cateReqList($_POST['cate']);
		break;

	case 'submitComment':
		submitComment($_POST['helpid'],$_POST['userID'],$_POST['username'],$_POST['comment']);
		break;

	case 'commentList':
		commentList($_POST['helpid']);
		break;

	case 'apply':
		apply($_POST['helpid'],$_POST['userID'],$_POST['username']);
		break;

	case 'applyList':
		applyList($_POST['helpid']);
		break;
		
	case 'choose':
		choose($_POST['helpid'],$_POST['userID'],$_POST['username'],$_POST['chooseID'],$_POST['chooseName']);
		break;

	case 'cancel':
		cancel($_POST['helpid'],$_POST['userID']);
		break;

	case 'confirm':
		confirm($_POST['helpid'],$_POST['chooseID']);
		break;

	case 'refuse':
		refuse($_POST['helpid'],$_POST['chooseID']);
		break;

	case 'scoreReq':
		scoreReq($_POST['scoreReq'],$_POST['helpid'],$_POST['chooseID'],$_POST['chooseName']);
		break;

	case 'scoreChoose':
		scoreChoose($_POST['scoreChoose'],$_POST['helpid'],$_POST['userID'],$_POST['requesterName']);
		break;
}

function scoreReq($score,$helpid,$cid,$cname)
{
	connectDB();

	if($helpid==null || $cid==null)
	{
		echo "false";
		return;
	}
	if($cid)
	{
		//先检验helpid chooseID  都对的情况下 是不是status==3 只有这种情况下才能让他scoreReq 
		$sql="select status,flagScoreReq,userID from help where id='$helpid' and chooseID='$cid'";
		$res=mysql_query($sql);
		if($res)
		{
			$row=mysql_fetch_array($res);
			if(!$row)
			{
				echo "这错误他妈就不应该可能出现";
				return;
			}
			if($row[0]!=3 )
			{
				echo "该状态不允许评论发布人";
				return;
			}
			if($row[1]!=0 )
			{
				echo "您已经给发布人打过分";
				return;
			}
			$userID=$row[2];
		}			

		$sql="UPDATE help SET ScoreReq=$score,flagScoreReq=1 WHERE id='$helpid' AND chooseID='$cid'";
		$res=mysql_query($sql);
		if(!res)
		{
			echo "false";
			return;
		}
		if($score>0){
			$sql="UPDATE user SET goodcomment=goodcomment+$score WHERE id='$userID'";
		}
		else if($score<=0)
		{

			$sql="UPDATE user SET badcomment=badcomment-$score WHERE id='$userID'";
		}
		$res=mysql_query($sql);
		//echo $res;
		if($res)
		{
			$sql="UPDATE friend SET point=point+$score WHERE frid='$userID' And foid='$cid'";
			$res=mysql_query($sql);
			if(!$res)
				echo "false";
			else
				echo "true";
		}
		else
		{
			echo "false";
		}
	}
	else
	{
		echo "false";
	}

}


function scoreChoose($score,$helpid,$uid,$uname)
{
	connectDB();

	if($helpid==null || $uid==null)
	{
		echo "false";
		return;
	}
	if($uid)
	{
		//先检验helpid uid  都对的情况下 是不是status==3 只有这种情况下才能让他scoreReq 
		$sql="select status,flagScoreChoose,chooseID from help where id='$helpid' and userID='$uid'";
		$res=mysql_query($sql);
		if($res)
		{
			$row=mysql_fetch_array($res);
			if(!$row)
			{
				echo "这错误他妈就不应该可能出现";
				return;
			}
			if($row[0]!=3 )
			{
				echo "该状态不允许评论承接人";
				return;
			}
			if($row[1]!=0 )
			{
				echo "您已经给承接人打过分";
				return;
			}
			$cid = $row[2];
		}			

		$sql="UPDATE help SET ScoreChoose=$score,flagScoreChoose=1 WHERE id='$helpid' AND userID='$uid'";
		$res=mysql_query($sql);
		//echo $res;
		if(!res)
		{
			echo "false";
			return;
		}

		if($score>0){
			$sql="UPDATE user SET goodcomment=goodcomment+$score WHERE id='$cid'";
		}
		else if($score<=0)
		{
			$sql="UPDATE user SET badcomment=badcomment-$score WHERE id='$cid'";
		}
		$res=mysql_query($sql);

		if($res)
		{
			$sql="UPDATE friend SET point=point+$score WHERE frid='$cid' And foid='$uid'";
			$res=mysql_query($sql);
			if(!$res)
				echo "false";
			else
				echo "true";
		}
		else
		{
			echo "false";
		}
	}
	else
	{
		echo "false";
	}

}

function reqHelp($title,$money,$cate,$description,$detail,$username,$userID)
{
	connectDB();
	
	//1请注意 我这里session并没起到作用 需要向一招 亚洲 求助
	//	session_start();
    //	$userID= $_SESSION['SuserID'];
	//2新的问题产生了 存入数据库的中文 看上去像乱码 当然了 我写个 “我的任务” 模块 看看网页下显示怎么样 我觉得有可能会正常显示
	
	//检查不为空
	if($title==null || $description==null || $detail==null || $title=="" || $description=="" || $detail=="" )
	{
		echo "false";
		return;
	}

	if($username && $userID)
	{
		//首先检查help数据库中 没有同标题的条目 有的话 则提示需要重新起名
		$sql="select id from help where title='$title' ";
		$res=mysql_query($sql);
		if($res)
		{
			if(mysql_fetch_array($res))
			{
				echo "存在同名标题 请修正标题";
				return;
			}
		}

		$sql="insert into help(title,userID,requesterName,money,cateid,description,detail) values('$title','$userID','$username','$money','$cate','$description','$detail')";
		$res=mysql_query($sql);
		if($res)
		{
			$sql="UPDATE user SET exp=exp+3 WHERE id='$userID'";
			$res=mysql_query($sql);
			if($res)
				echo "true";
			else
				echo "false";
		}
		else
		{
			echo "false";
		}
	}
	else
	{
		echo "false";
	}
}

function getHelp($id)
{
	global $cateArr;
	global $statusArr;
	connectDB();
	if($id)
	{
		$sql="select * from help where id='$id' ";
		$res=mysql_query($sql);
		//echo $res;
		if(!$res)
		{
			echo "false";
		}
		else
		{
			
			$row=mysql_fetch_array($res);
			$description=str_replace("\r\n","<br>",$row['description']);
			$description=str_replace("\n","<br>",$description);
			$detail=str_replace("\r\n","<br>",$row['detail']);
			$detail=str_replace("\n","<br>",$detail);
			$help="{id:'".$row["id"]."',title:'".$row["title"]."',money:'".$row["money"]."',userID:'".$row["userID"]."',requesterName:'".$row["requesterName"]."',cate:'".$cateArr[$row["cateid"]]."',description:'".$description."',detail:'".$detail."',status:'".$row["status"]."',statusstr:'".$statusArr[$row["status"]]."',chooseID:'".$row["chooseID"]."',chooseName:'".$row["chooseName"]."',flagScoreReq:'".$row["flagScoreReq"]."',ScoreReq:'".$row["ScoreReq"]."',flagScoreChoose:'".$row["flagScoreChoose"]."',ScoreChoose:'".$row["ScoreChoose"]."'}";
			//return $help;
			echo $help;
			
			//echo "true";
		}
	}
	else
	{
		echo "false";
	}
}

function iReqList($username)
{
	global $cateArr;
	global $statusArr;
	connectDB();
	//再次重申 我很清楚name不如id 但是我不会 好心人能不能帮我实现以下session 我就不需要这么窘迫了

	if($username)
	{
		$sql="select * from help where requesterName='$username' ";
		$res=mysql_query($sql);	
		$iReqList="[";
					
		$i=0;	
		if(!$res)
		{
			return false;
		}
		while($row=mysql_fetch_array($res))
		{
			//将textarea中上传的文本中的换行符替换为"<br>"
			$description=str_replace("\r\n","<br>",$row['description']);
			$description=str_replace("\n","<br>",$description);
			$detail=str_replace("\r\n","<br>",$row['detail']);
			$detail=str_replace("\n","<br>",$detail);
	
			$rowStr="{id:'".$row["id"]."',title:'".$row["title"]."',money:'".$row["money"]."',userID:'".$row["userID"]."',requesterName:'".$row["requesterName"]."',cate:'".$cateArr[$row["cateid"]]."',description:'".$description."',detail:'".$detail."',status:'".$row["status"]."',statusstr:'".$statusArr[$row["status"]]."',chooseID:'".$row["chooseID"]."',chooseName:'".$row["chooseName"]."',flagScoreReq:'".$row["flagScoreReq"]."',ScoreReq:'".$row["ScoreReq"]."',flagScoreChoose:'".$row["flagScoreChoose"]."',ScoreChoose:'".$row["ScoreChoose"]."'},";
			$iReqList.=$rowStr;		
			$i++;
		}
		if($i>0)
		{
			 $iReqList=substr_replace($iReqList,'',-1,1);
		}
		$iReqList.="]";
		echo $iReqList;

	}
	else
	{
		echo "false";
	}
	//echo "我当然知道 不用id而用name来存储 发布人 是有点傻逼了 以后还是会修改成userID的 ";	
}

function allReqList()
{
	global $cateArr;
	global $statusArr;
	connectDB();
	$sql="select * from help";
	$res=mysql_query($sql);	
	$allReqList="[";
	$i=0;	
	while($row=mysql_fetch_array($res))
	{	
		//将textarea中上传的文本中的换行符替换为"<br>"
		$description=str_replace("\r\n","<br>",$row['description']);
		$description=str_replace("\n","<br>",$description);
		$detail=str_replace("\r\n","<br>",$row['detail']);
		$detail=str_replace("\n","<br>",$detail);
		
		$cateid=$row["cateid"];//
		$cate = $cateArr["$cateid"];
		$rowStr="{id:'".$row["id"]."',title:'".$row["title"]."',money:'".$row["money"]."',userID:'".$row["userID"]."',requesterName:'".$row["requesterName"]."',cate:'".$cate."',description:'".$description."',detail:'".$detail."',status:'".$row["status"]."',statusstr:'".$statusArr[$row["status"]]."',chooseID:'".$row["chooseID"]."',chooseName:'".$row["chooseName"]."',flagScoreReq:'".$row["flagScoreReq"]."',ScoreReq:'".$row["ScoreReq"]."',flagScoreChoose:'".$row["flagScoreChoose"]."',ScoreChoose:'".$row["ScoreChoose"]."'},";
		$allReqList.=$rowStr;		
		$i++;
	}
	if($i>0)
	{
		$allReqList=substr_replace($allReqList,'',-1,1);
	}
	$allReqList.="]";
	echo $allReqList;
}

function iApplyList($uID)
{
	global $cateArr;
	global $statusArr;
	connectDB();

	if($uID)
	{
		$sql="select * from help left join apply a on a.helpID=id  where a.uID='$uID' ";
		$res=mysql_query($sql);	
		$helpList="[";
					
		$i=0;	
		if(!$res)
		{
			return false;
		}
		while($row=mysql_fetch_array($res))
		{
			//将textarea中上传的文本中的换行符替换为"<br>"
			$description=str_replace("\r\n","<br>",$row['description']);
			$description=str_replace("\n","<br>",$description);
			$detail=str_replace("\r\n","<br>",$row['detail']);
			$detail=str_replace("\n","<br>",$detail);
	
			$rowStr="{id:'".$row["id"]."',title:'".$row["title"]."',money:'".$row["money"]."',userID:'".$row["userID"]."',requesterName:'".$row["requesterName"]."',cate:'".$cateArr[$row["cateid"]]."',description:'".$description."',detail:'".$detail."',status:'".$row["status"]."',statusstr:'".$statusArr[$row["status"]]."',chooseID:'".$row["chooseID"]."',chooseName:'".$row["chooseName"]."',flagScoreReq:'".$row["flagScoreReq"]."',ScoreReq:'".$row["ScoreReq"]."',flagScoreChoose:'".$row["flagScoreChoose"]."',ScoreChoose:'".$row["ScoreChoose"]."'},";
			$helpList.=$rowStr;		
			$i++;
		}
		if($i>0)
		{
			 $helpList=substr_replace($helpList,'',-1,1);
		}
		$helpList.="]";
		echo $helpList;

	}
	else
	{
		echo "false";
	}
	//echo "我当然知道 不用id而用name来存储 发布人 是有点傻逼了 以后还是会修改成userID的 ";	
}

function iChooseList($chooseID)
{
	global $cateArr;
	global $statusArr;
	connectDB();
	//再次重申 我很清楚name不如id 但是我不会 好心人能不能帮我实现以下session 我就不需要这么窘迫了

	if($chooseID)
	{
		$sql="select * from help where chooseID='$chooseID' ";
		$res=mysql_query($sql);	
		$helpList="[";
					
		$i=0;	
		if(!$res)
		{
			return false;
		}
		while($row=mysql_fetch_array($res))
		{
			//将textarea中上传的文本中的换行符替换为"<br>"
			$description=str_replace("\r\n","<br>",$row['description']);
			$description=str_replace("\n","<br>",$description);
			$detail=str_replace("\r\n","<br>",$row['detail']);
			$detail=str_replace("\n","<br>",$detail);
	
			$rowStr="{id:'".$row["id"]."',title:'".$row["title"]."',money:'".$row["money"]."',userID:'".$row["userID"]."',requesterName:'".$row["requesterName"]."',cate:'".$cateArr[$row["cateid"]]."',description:'".$description."',detail:'".$detail."',status:'".$row["status"]."',statusstr:'".$statusArr[$row["status"]]."',chooseID:'".$row["chooseID"]."',chooseName:'".$row["chooseName"]."',flagScoreReq:'".$row["flagScoreReq"]."',ScoreReq:'".$row["ScoreReq"]."',flagScoreChoose:'".$row["flagScoreChoose"]."',ScoreChoose:'".$row["ScoreChoose"]."'},";
			$helpList.=$rowStr;		
			$i++;
		}
		if($i>0)
		{
			 $helpList=substr_replace($helpList,'',-1,1);
		}
		$helpList.="]";
		echo $helpList;

	}
	else
	{
		echo "false";
	}
	//echo "我当然知道 不用id而用name来存储 发布人 是有点傻逼了 以后还是会修改成userID的 ";	
}

function iFocusList($userID)
{
	global $cateArr;
	global $statusArr;
	connectDB();
	//再次重申 我很清楚name不如id 但是我不会 好心人能不能帮我实现以下session 我就不需要这么窘迫了
	//echo "userID = $userID";
	//echo "i fuck dan";
	if($userID)
	{
		$sql="select h.* from help as h LEFT JOIN focuscate as f ON (f.cateID=h.cateid) where f.userID='$userID' ";
		$res=mysql_query($sql);	
		$helpList="[";
					
		$i=0;	
		if(!$res)
		{
			echo "$res";
			return false;
		}
		while($row=mysql_fetch_array($res))
		{
			//将textarea中上传的文本中的换行符替换为"<br>"
			$description=str_replace("\r\n","<br>",$row['description']);
			$description=str_replace("\n","<br>",$description);
			$detail=str_replace("\r\n","<br>",$row['detail']);
			$detail=str_replace("\n","<br>",$detail);
	
			$rowStr="{id:'".$row["id"]."',title:'".$row["title"]."',money:'".$row["money"]."',userID:'".$row["userID"]."',requesterName:'".$row["requesterName"]."',cate:'".$cateArr[$row["cateid"]]."',description:'".$description."',detail:'".$detail."',status:'".$row["status"]."',statusstr:'".$statusArr[$row["status"]]."',chooseID:'".$row["chooseID"]."',chooseName:'".$row["chooseName"]."',flagScoreReq:'".$row["flagScoreReq"]."',ScoreReq:'".$row["ScoreReq"]."',flagScoreChoose:'".$row["flagScoreChoose"]."',ScoreChoose:'".$row["ScoreChoose"]."'},";
			$helpList.=$rowStr;		
			$i++;
		}
		if($i>0)
		{
			 $helpList=substr_replace($helpList,'',-1,1);
		}
		$helpList.="]";
		echo $helpList;

	}
	else
	{
		echo "false";
	}
	//echo "我当然知道 不用id而用name来存储 发布人 是有点傻逼了 以后还是会修改成userID的 ";	
}

function friendReqList($userID)
{
	global $cateArr;
	global $statusArr;
	connectDB();
	//再次重申 我很清楚name不如id 但是我不会 好心人能不能帮我实现以下session 我就不需要这么窘迫了
	//echo "userID = $userID";
	//echo "i fuck dan";
	if($userID)
	{
		$sql="select h.* from help as h LEFT JOIN friend as f ON (f.frid=h.userID) where f.foid='$userID' ";
		$res=mysql_query($sql);	
		$helpList="[";
					
		$i=0;	
		if(!$res)
		{
			echo "$res";
			return false;
		}
		while($row=mysql_fetch_array($res))
		{
			//将textarea中上传的文本中的换行符替换为"<br>"
			$description=str_replace("\r\n","<br>",$row['description']);
			$description=str_replace("\n","<br>",$description);
			$detail=str_replace("\r\n","<br>",$row['detail']);
			$detail=str_replace("\n","<br>",$detail);
	
			$rowStr="{id:'".$row["id"]."',title:'".$row["title"]."',money:'".$row["money"]."',userID:'".$row["userID"]."',requesterName:'".$row["requesterName"]."',cate:'".$cateArr[$row["cateid"]]."',description:'".$description."',detail:'".$detail."',status:'".$row["status"]."',statusstr:'".$statusArr[$row["status"]]."',chooseID:'".$row["chooseID"]."',chooseName:'".$row["chooseName"]."',flagScoreReq:'".$row["flagScoreReq"]."',ScoreReq:'".$row["ScoreReq"]."',flagScoreChoose:'".$row["flagScoreChoose"]."',ScoreChoose:'".$row["ScoreChoose"]."'},";
			$helpList.=$rowStr;		
			$i++;
		}
		if($i>0)
		{
			 $helpList=substr_replace($helpList,'',-1,1);
		}
		$helpList.="]";
		echo $helpList;

	}
	else
	{
		echo "false";
	}
	//echo "我当然知道 不用id而用name来存储 发布人 是有点傻逼了 以后还是会修改成userID的 ";	
}
function cateReqList($cate)
{
	global $cateArr;
	global $statusArr;
	connectDB();
	//再次重申 我很清楚name不如id 但是我不会 好心人能不能帮我实现以下session 我就不需要这么窘迫了

	if($cate)
	{
		$sql="select * from help where cateid='$cate' ";
		$res=mysql_query($sql);	
		$helpList="[";
					
		$i=0;	
		if(!$res)
		{
			return false;
		}
		while($row=mysql_fetch_array($res))
		{
			//将textarea中上传的文本中的换行符替换为"<br>"
			$description=str_replace("\r\n","<br>",$row['description']);
			$description=str_replace("\n","<br>",$description);
			$detail=str_replace("\r\n","<br>",$row['detail']);
			$detail=str_replace("\n","<br>",$detail);
	
			$rowStr="{id:'".$row["id"]."',title:'".$row["title"]."',money:'".$row["money"]."',userID:'".$row["userID"]."',requesterName:'".$row["requesterName"]."',cate:'".$cateArr[$row["cateid"]]."',description:'".$description."',detail:'".$detail."',status:'".$row["status"]."',statusstr:'".$statusArr[$row["status"]]."',chooseID:'".$row["chooseID"]."',chooseName:'".$row["chooseName"]."',flagScoreReq:'".$row["flagScoreReq"]."',ScoreReq:'".$row["ScoreReq"]."',flagScoreChoose:'".$row["flagScoreChoose"]."',ScoreChoose:'".$row["ScoreChoose"]."'},";
			$helpList.=$rowStr;		
			$i++;
		}
		if($i>0)
		{
			 $helpList=substr_replace($helpList,'',-1,1);
		}
		$helpList.="]";
		echo $helpList;

	}
	else
	{
		echo "false";
	}
	//echo "我当然知道 不用id而用name来存储 发布人 是有点傻逼了 以后还是会修改成userID的 ";	
}

function submitComment($helpid,$userID,$username,$comment)
{
	connectDB();

	if($helpid==null || $userID==null || $username==null || $comment=="" || $comment==null )
	{
		echo "false";
		return;
	}

	if($username && $userID)
	{
		$sql="insert into comment(helpID,uID,uName,comment) values('$helpid','$userID','$username','$comment')";
		$res=mysql_query($sql);
		//echo $res;
		if($res)
		{
			echo "true";
		}
		else
		{
			echo "false";
		}
	}
	else
	{
		echo "false";
	}
}

function commentList($helpid)
{
	connectDB();
	//再次重申 我很清楚name不如id 但是我不会 好心人能不能帮我实现以下session 我就不需要这么窘迫了

	if($helpid)
	{
		$sql="select * from comment where helpID='$helpid' ";
		$res=mysql_query($sql);	
		$cList="[";
					
		$i=0;	
		if(!$res)
		{
			return false;
		}
		while($row=mysql_fetch_array($res))
		{
			//将textarea中上传的文本中的换行符替换为"<br>"
			$comment=str_replace("\r\n","<br>",$row['comment']);
			$comment=str_replace("\n","<br>",$comment);
	
			$rowStr="{id:'".$row["cID"]."',uID:'".$row["uID"]."',uName:'".$row["uName"]."',comment:'".$comment."'},";
			$cList.=$rowStr;		
			$i++;
		}
		if($i>0)
		{
			 $cList=substr_replace($cList,'',-1,1);
		}
		$cList.="]";
		echo $cList;

	}
	else
	{
		echo "false";
	}
}

function apply($helpid,$userID,$username)
{
	connectDB();

	if($helpid==null || $userID==null || $username==null)
	{
		echo "false";
		return;
	}

	if($username && $userID)
	{
		//检查申请人是不是request本人
		//客户端可以检查 但服务器这边是不是应该再确认一下
		//1 检查我是不是request本人  理论上 客户端已经过滤了
		//2 检查我是不是已经申请过了	理论上 这个过滤只有在服务器端进行

		$sql="select aID from apply where uName='$username' and uID='$userID' and helpID='$helpid'";
		$res=mysql_query($sql);
		//用户已参与
		if($res)
		{
			if(mysql_fetch_array($res))
			{
				echo "您已申请过了";
				return;
			}
		}

		//检查任务的状态 如果任务状态已经>2则无法再申请
		//检查任务的发布人 如果发布人就是自己也无法申请
		$sql="select status,userID from help where id='$helpid' ";
		$res=mysql_query($sql);
		//echo "$res";
		if(!$res)
		{
			echo "false";//没有关于该helpid的求助 这个真的不可思议 应该不会出现这种情况	
			return;
		}
		else
		{
			$row = mysql_fetch_array($res);
			$status = $row[0];
			if($status>2)
			{
				echo "该任务已经被别人成功领取";
				return;
			}
			$reqid=$row[1];//发布人的id 给下面检查申请人和发布人是不是好友用的
			if($userID==$row[1])
			{
				echo "你妈逼别不要脸了 自己发的任务自己接个逼啊";//理论上来讲 客户端应该可以过滤这种可能性 所以我骂得重一点
				return;
			}
		}

		$sql="insert into apply(helpID,uID,uName) values('$helpid','$userID','$username')";
		$res=mysql_query($sql);
		//echo "reqid=$reqid   userID= $userID   ";
		if($res)
		{
			$sql="UPDATE user SET exp=exp+2 WHERE id='$userID'";
			$res=mysql_query($sql);
			if(!$res)
				echo "false";

			$sql="UPDATE friend SET point=point+1 WHERE frid='$reqid' And foid='$userID'";
			$res=mysql_query($sql);
			if(!$res)
				echo "false";
			else
				echo "true";
			
		}
		else
		{
			echo "false";
		}
	}
	else
	{
		echo "false";
	}
}

function applyList($helpid)
{
	connectDB();
	//再次重申 我很清楚name不如id 但是我不会 好心人能不能帮我实现以下session 我就不需要这么窘迫了

	if($helpid)
	{
		$sql="select * from apply where helpID='$helpid' ";
		$res=mysql_query($sql);	
		$aList="[";
					
		$i=0;	
		if(!$res)
		{
			return false;
		}
		while($row=mysql_fetch_array($res))
		{
			$rowStr="{id:'".$row["aID"]."',uID:'".$row["uID"]."',uName:'".$row["uName"]."'},";
			$aList.=$rowStr;		
			$i++;
		}
		if($i>0)
		{
			 $aList=substr_replace($aList,'',-1,1);
		}
		$aList.="]";
		echo $aList;

	}
	else
	{
		echo "false";
	}
}

function choose($helpid,$userID,$username,$chooseID,$chooseName)
{
	connectDB();

	if($helpid==null || $userID==null || $username==null)
	{
		echo "false";
		return;
	}
	//echo "helpid = $helpid userID = $userID username = $username  ";
	if($username && $userID)
	{
		//先检验helpid userID username 都对的情况下 是不是status==0 只有这种情况下才能让他更改 否则就是已经指定给了别人
		$sql="select status from help where id='$helpid' and userID='$userID' and requesterName='$username'";
		$res=mysql_query($sql);
		if($res)
		{
			$row=mysql_fetch_array($res);
			if($row[0]!=0 && $row[0]!=2)
			{
				echo "该任务已被指定";
				return;
			}
			//echo ("row[0]: $row    id=$helpid    userID=$userID    username=$username   ");
			//return;
		}			

		//更改help的 status chooseID chooseName
		$sql="UPDATE help SET status=1,chooseID='$chooseID',chooseName='$chooseName' WHERE id='$helpid'";
		//$sql="insert into apply(helpID,uID,uName) values('$helpid','$userID','$username')";
		$res=mysql_query($sql);
		//echo $res;
		if($res)
		{
			echo "true";
		}
		else
		{
			echo "false";
		}
	}
	else
	{
		echo "false";
	}
}

function cancel($helpid,$userID)
{
	connectDB();

	if($helpid==null || $userID==null)
	{
		echo "false";
		return;
	}
	//echo "helpid = $helpid userID = $userID   ";

	if($userID)
	{
		//先检验helpid userID  都对的情况下 是不是status==1 只有这种情况下才能让他取消 
		//否则要不就是已经被别人接受了 要不就是还没有指定给谁(或者指定了但是已经取消)
		$sql="select status from help where id='$helpid' and userID='$userID'";
		$res=mysql_query($sql);
		if($res)
		{
			$row=mysql_fetch_array($res);
			if($row[0]!=1)
			{
				echo "该任务的状态不处于等待接收的状态 所以你不能反悔";
				return;
			}
			//echo ("row[0]: $row    id=$helpid    userID=$userID    username=$username   ");
			//return;
		}			

		//更改help的 status lockto locktoName
		$sql="UPDATE help SET status=0,chooseID=0,chooseName=null WHERE id='$helpid' AND userID='$userID'";
		//$sql="insert into apply(helpID,uID,uName) values('$helpid','$userID','$username')";
		$res=mysql_query($sql);
		//echo $res;
		if($res)
		{
			echo "true";
		}
		else
		{
			echo "false";
		}
	}
	else
	{
		echo "false";
	}
}


function confirm($helpid,$chooseID)
{
	connectDB();

	if($helpid==null || $chooseID==null)
	{
		echo "false";
		return;
	}
	//echo "helpid = $helpid userID = $userID   ";

	if($chooseID)
	{
		//先检验helpid chooseID  都对的情况下 是不是status==1 只有这种情况下才能让他confirm 
		//否则要不就是已经被别人接受了 要不就是还没有指定给谁(或者指定了但是已经取消)
		$sql="select status,userID from help where id='$helpid' and chooseID='$chooseID'";
		$res=mysql_query($sql);
		if($res)
		{
			$row=mysql_fetch_array($res);
			if(!$row)
			{
				echo "这错误他妈就不应该可能出现";
				return;
			}
			if($row[0]!=1)
			{
				echo "该任务的状态不处于等待确认的状态 所以你不能确认";
				return;
			}
			$reqid = $row[1];//为下面增加活跃度用的
		}			

		$sql="UPDATE help SET status=3 WHERE id='$helpid' AND chooseID='$chooseID'";
		//$sql="insert into apply(helpID,uID,uName) values('$helpid','$userID','$username')";
		$res=mysql_query($sql);
		//echo $res;
		if($res)
		{
			$sql="UPDATE user SET exp=exp+5 WHERE id='$chooseID'";
			$res=mysql_query($sql);
			if(!$res)
				echo "false";

			$sql="UPDATE user SET exp=exp+5 WHERE id='$reqid'";
			$res=mysql_query($sql);
			if(!$res)
				echo "false";

			$sql="UPDATE friend SET point=point+1 WHERE frid='$reqid' And foid='$chooseID'";
			$res=mysql_query($sql);
			if(!$res)
				echo "false";

			$sql="UPDATE friend SET point=point+1 WHERE frid='$chooseID' And foid='$reqid'";
			$res=mysql_query($sql);
			if(!$res)
				echo "false";
			else
				echo "true";
		}
		else
		{
			echo "false";
		}
	}
	else
	{
		echo "false";
	}
}

function refuse($helpid,$chooseID)
{
	connectDB();

	if($helpid==null || $chooseID==null)
	{
		echo "false";
		return;
	}
	if($chooseID)
	{
		//先检验helpid chooseID  都对的情况下 是不是status==1 只有这种情况下才能让他refuse 
		//否则要不就是已经被别人接受了 要不就是还没有指定给谁(或者指定了但是已经取消)
		$sql="select status from help where id='$helpid' and chooseID='$chooseID'";
		$res=mysql_query($sql);
		if($res)
		{
			$row=mysql_fetch_array($res);
			if(!$row)
			{
				echo "这错误他妈就不应该可能出现";
				return;
			}
			if($row[0]!=1)
			{
				echo "该任务的状态不处于等待确认的状态 所以你不能拒绝";
				return;
			}
		}			

		$sql="UPDATE help SET status=2,chooseID=0,chooseName=null WHERE id='$helpid' AND chooseID='$chooseID'";
		$res=mysql_query($sql);
		//echo $res;
		if($res)
		{
			echo "true";
		}
		else
		{
			echo "false";
		}
	}
	else
	{
		echo "false";
	}
}
?>
